;
/*
 * Format functions
 * by Thuy
 */
;(function(W, D, $) {

	$.extend({
		'version': '1.2'
	
		, 'isBlank': function(str) {
			return (typeof(str) == 'undefined' || str == null || $.trim(str) == '');
		}
		
		, 'isEmail': function(str) {
			return str.search(/^[\w\.=-]+@[\w\.-]+\.[\w]{2,9}$/) != -1;
		}
		
		, 'isPhone': function(str) {
			return str.search(/^(\+[1-9][0-9]*(\([0-9]*\)|-[0-9]*-))?[0]?[1-9][0-9\- ]*$/) != -1;
		}
		
		, 'isNum': function(str) {
			return str.search(/^-?(\d+|(\d{1,3}(,\d{3})*))((,|\.)\d+)?$/) != -1;
		}
		
		, 'URLNoCache': function(url) {
			if (url == null) {
				return '';
			}
			url = url.toString();
			if ($.trim(url) == '') {
				return '';
			}
			var sequence = String(Math.random()).replace('0.', '');
			var params = url.split('rnd=');
			if (params.length == 2) {
				var rnds = params[1].split('&');
				return params[0] + 'rnd=' + sequence + (rnds.length == 2 ? '&' + rnds[1] : '');
			} else {
				return url + (url.indexOf('?') == -1 ? '?' : '&') + 'rnd=' + sequence;
			}
		}
		
		, 'goTo': function(url) {
			W.location.href = url;
		}
	});
	
	$.fn.extend({
		'scrollTo': function(selector) {
			this.scrollTop(
				$(selector).offset().top
				 - this.offset().top
				 + this.scrollTop()
			);
			
			return this;
		}
		, 'noWrap': function() {
			this.each(function() {
				$(this).html($(this).text().replace(/\s+/g, '&nbsp;'));
			});
			return this;
		}
		, 'toggleClass': function(name) {
			if (typeof(name) != 'undefined' && name != null && name != '') {
				this.each(function() {
					if (!$(this).hasClass(name)) {
						$(this).addClass(name);
					} else {
						$(this).removeClass(name);
					}
				});
			}
			return this;
		}
		
		/* special input validation */
		, 'formcontrol': function() {
			this.find('input[type=text], input[type=password], input[type=checkbox], input[type=radio], textarea, select').each(function() {
				$(this).val($(this).data('default'));
				if (typeof($(this).data('require')) != 'undefined' && $(this).data('require') != 'none') {
					$(this).data('valid', 'failed');
				} else {
					$(this).data('valid', 'checked');
				}
				
				$(this).bind({
					'focus': function() {
						$(this).focusin();
					}
					, 'blur': function() {
						$(this).focusout();
					}
					, 'keyup change': function() {
						$(this).validate(true);
					}
				});
			});
			
			return this;
		}
		
		, 'focusin': function() {
			this.each(function() {
				if ($.trim($(this).val()) == $(this).data('default')) {
					$(this)
						.val('')
						.hideWarning();
				}
			});
			return this;
		}
		
		, 'focusout': function() {
			this.each(function() {
				$(this).val($.trim($(this).val()));
				if ($.isBlank($(this).val())) {
					$(this)
						.val($(this).data('default'))
						.hideWarning();
				}
				$(this).validate();
			});
			return this;
		}
		
		, 'validate': function(iftrue) {
			this.each(function() {
				var val = $.trim($(this).val());
				var condition = $(this).data('require') || '';
				
				if (condition == 'none' || condition == '') {
					$(this).hideWarning();
				}
				
				if (condition == 'required') {
					if (typeof(iftrue) != 'undefined' && iftrue != null) {
						if (!$.isBlank(val) && val != $(this).data('default')) {
							$(this).hideWarning();
						}
						return;
					}
					if ($.isBlank(val) || val == $(this).data('default')) {
						$(this).showWarning();
					} else {
						$(this).hideWarning();
					}
				}
				
				if (condition == 'email') {
					if (typeof(iftrue) != 'undefined' && iftrue != null) {
						if ($.isEmail(val)) {
							$(this).hideWarning();
						}
						return;
					}
					if (!$.isEmail(val)) {
							$(this).showWarning();
						} else {
							$(this).hideWarning();
						}
				}
				
				if (condition.indexOf('equal') != -1) {
					var target_name = condition.replace('equal:', '');
					var target = $(this.form).find('input[name=' + target_name + '], .' + target_name);

					if (target.length == 0) {
						return;
					}
					if (typeof(iftrue) != 'undefined' && iftrue != null) {
						if (val == target.val()) {
							$(this).hideWarning();
						}
						return;
					}
					if (val != target.val() && !$.isBlank(target.val())) {
							$(this).showWarning();
						} else {
							$(this).hideWarning();
						}
				}
				
				if (condition.indexOf('greater') != -1) {
					var target_name = condition.replace('greater:', '');
					var target = $(this.form).find('input[name=' + target_name + '], .' + target_name);

					if (target.length == 0) {
						return;
					}
					if (typeof(iftrue) != 'undefined' && iftrue != null) {
						if (val == target.val()) {
							$(this).hideWarning();
						}
						return;
					}
					if (val <= target.val() && !$.isBlank(target.val())) {
							$(this).showWarning();
						} else {
							$(this).hideWarning();
						}
				}
				
				if (condition == 'phone') {
					if (typeof(iftrue) != 'undefined' && iftrue != null) {
						if ($.isPhone(val)) {
							$(this).hideWarning();
						}
						return;
					}
					if (!$.isPhone(val)) {
							$(this).showWarning();
						} else {
							$(this).hideWarning();
						}
				}
				
				if (condition == 'number') {
					if (typeof(iftrue) != 'undefined' && iftrue != null) {
						if ($.isNum(val)) {
							$(this).hideWarning();
						}
						return;
					}
					if (!$.isNum(val)) {
							$(this).showWarning();
						} else {
							$(this).hideWarning();
						}
				}
			});
			return this;
		}
		
		, 'showWarning': function() {
			this
				.hideWarning()
				.each(function() {
				var target = $(this);
				if (target.next('label').length == 1) {
					target = target.next('label');
				}
				
				$('<span />', {
					'class': 'input-warning'
					, 'text': $(this).data('alert') || 'Missing value ...'
				}).insertAfter(target);
				
				$(this)
					.attr('title', $(this).data('alert'))
					.data('valid', 'failed')
					.addClass('required');
			});
			
			return this;
		}
		
		, 'hideWarning': function() {
			this.each(function() {
				$(this).next('span.input-warning').remove();
				$(this).next('label').next('span.input-warning').remove();
				$(this)
					.removeAttr('title')
					.data('valid', 'checked')
					.removeClass('required');
			});
			
			return this;
		}
		
		, 'formcheck': function() {
			var valid = true;
			this.find('input[type=text], input[type=password], input[type=checkbox], input[type=radio], textarea, select').each(function() {
				$(this).validate();
				if ($(this).data('valid') == 'failed') {
					valid = false;
					$(this).showWarning();
				}
			});
			return valid;
		}
	});
	
})(window, document, jQuery);

;$(function($){
	if (typeof($.datepicker) == 'undefined') {
		return;
	}
	$.datepicker.regional['vn'] = {
		closeText: 'Đóng',
		prevText: 'Trước',
		nextText: 'Sau',
		currentText: 'Aujourd\'hui',
		monthNames: ['Tháng 1','Tháng 2','Tháng 3','Tháng 4','Tháng 5','Tháng 6','Tháng 7','Tháng 8','Tháng 9','Tháng 10','Tháng 11','Tháng 12'],
		monthNamesShort: ['Thg 1','Thg 2','Thg 3','Thg 4','Thg 5','Thg 6','Thg 7','Thg 8','Thg 9','Thg 10','Thg 11','Thg 12'],
		dayNames: ['Chủ Nhật','Thứ 2','Thứ 3','Thứ 4','Thứ 5','Thứ 6','Thứ 7'],
		dayNamesShort: ['CNh','Th2','Th3','Th4','Th5','Th6','Th7'],
		dayNamesMin: ['CN','T2','T3','T4','T5','T6','T7'],
		weekHeader: 'Tuần',
		dateFormat: 'dd/mm/yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		changeMonth: true,
		changeYear: true,
		yearRange: '-100y:c+nn',
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['vn']);
});

;(function(W, D, $) {
;({
	'sysparam' : {
		'version': '1.0'
		, 'null': null
	}
	
	, 'init_Interface': function(s) {
		/* home page */
		if (typeof(Masonry) != 'undefined') {
			$('#itemContainer').masonry({
				'columnWidth' 		: 220
				, 'itemSelector'	: '.itemDetail'
				, 'gutter'			: 12
			});
				
			$(W).scroll(function() {
				if ($(W).scrollTop() + $(W).height() == $(D).height()) {
					if ($('#itemContainer').length == 1 && !$('#itemContainer').hasClass('ajax-loading')) {
						$('#itemContainer').addClass('ajax-loading');
						$.ajax({
							'url': 'ajax-content.html'
							, 'dataType': 'html'
							, 'success': function(data) {
								var holder = $('#ajax-content-holder');
								if (holder.length == 0) {
									holder = $('<div/>', {
										'id': 'ajax-content-holder'
										, 'css': {
											'width': '1px'
											, 'height': '1px'
											, 'overflow': 'hidden'
										}
									}).appendTo('body');
								}
								holder.append(data);
								var items = holder.find('.itemDetail');
								$('#itemContainer').append(items).masonry('appended', items);
								
								$('#itemContainer').removeClass('ajax-loading');
							}
						});
					}
				}
			});
		}
		
		
		/* directory menu */
		$('.directory-toggle').click(function(evt) {
			evt.preventDefault();
			if ($('.directories').hasClass('directory-opened')) {
				$('.directories').removeClass('directory-opened');
			} else {
				$('.directories').addClass('directory-opened');
			}
		});
		$(D).click(function(evt) {
			var target = $(evt.target);
			if (target.hasClass('directory-toggle') || target.hasClass('directory') || target.parents('.directory-toggle').length == 1 || target.parents('.directories').length == 1) {
				return;
			}
			$('.directories').removeClass('directory-opened');
		});
	}
	
	, 'init_Form': function(s) {
		/* global form controls */
		if (typeof($.datepicker) != 'undefined') {
			$('input[name=dangky_ngaysinh]').datepicker();
		}
		
		/* bid form */
		$('input[name=frmItemReserve], input[name=frmItemStart], input[name=frmItemSellnow]').keydown(function(evt) {
			if (evt.keyCode == 109 || evt.keyCode == 189) {
				evt.stopPropagation();
				evt.preventDefault();
			}
		}).inputmask();
		$('input[name=frmItemReserve]').keyup(function() {
			if ($('input[name=frmItemNoReserve]').prop('checked') == true) {
				$('input[name=frmItemStart]').val($(this).val());
			}
		});
		$('input[name=frmItemNoReserve]').click(function() {
			if ($(this).prop('checked') == true) {
				$('input[name=frmItemStart]')
					.data('require', 'none')
					.attr('disabled', 'disabled')
					.val('')
					.validate();
			} else {
				$('input[name=frmItemStart]')
					.data('require', 'number')
					.removeAttr('disabled')
					.val('')
					.validate();
			}
		});
		
		
		/* Login form */
		$('form')
			.formcontrol()
			.submit(function(evt) {
				if ($(this).formcheck() === false) {
					evt.preventDefault();
					return;
				}
			});
	}
	
	
	
	, 'load_Splash': function () {
		
	}
	
	, 'initialize': function() {
		var scope = this;
		$(D).ready(function() {
			$.each(scope, function(fn_name, fn) {
				if (String(fn_name).indexOf('init_') == 0) {
					try {
						(scope[fn_name])(scope);
					} catch (err) {};
				}
			});
		});
		$(W).load(function() {
			$.each(scope, function(fn_name, fn) {
				if (String(fn_name).indexOf('load_') == 0) {
					try {
						(scope[fn_name])(scope);
					} catch (err) {};
				}
			});
		});
	}
}).initialize();

})(window, window.document, jQuery);